import thunkMiddleware from 'redux-thunk';
// import createLogger from 'redux-logger';
import {createStore, applyMiddleware, compose} from 'redux';
import rootReducer from '../reducers';

const isProduction = process.env.NODE_ENV === 'production';
// const loggerMiddleware = createLogger();
const middlewares = [
  thunkMiddleware // 允许我们 dispatch() 函数
];

// 一个很便捷的 middleware，用来打印 action 日志
// if (!isProduction) middlewares.push(loggerMiddleware);

const composeEnhancers =
  typeof window === 'object' && (
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
        // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
      })
    : compose
  );

const enhancer = composeEnhancers(
  applyMiddleware(...middlewares),
  // other store enhancers if any
);

const store = createStore(rootReducer, enhancer);

if (module.hot) {
  // Enable Webpack hot module replacement for reducers
  module.hot.accept('../reducers', () => {
    const nextRootReducer = require('../reducers');
    store.replaceReducer(nextRootReducer);
  });
}

export default store;
